package edu.ccut.saturn.studio.wfg.dialog.userdialog.treeoperate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.ccut.saturn.studio.database.model.DBConnection;


public class Untils {
	private static Untils unitls = null;
	Connection conn = null;
	private PreparedStatement preparedStatement = null;
	
	
	public static Untils getInstance(){
		if(unitls==null){
			return unitls = new Untils();
		}
		return unitls;
	}
	
	public ResultSet getSelectResult(String sql,String parameter[],DBConnection connection){
		ResultSet resultSet = null;
		try {
			conn = connection.getConnectionSync();
			preparedStatement = conn.prepareStatement(sql);
			if(parameter!=null){
				for(int i = 0;i<parameter.length;i++){
					preparedStatement.setString(i+1, parameter[i]);
				}
			}
			resultSet = preparedStatement.executeQuery();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultSet;
	}
	
	public void closeConnent(){
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void closeStatement(){
		if(preparedStatement!=null){
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
